package cn.echo.modules.sys.controller.usercontroller;


import cn.echo.modules.sys.entity.Site;
import cn.echo.modules.sys.entity.User;

import cn.echo.modules.sys.service.UserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("user")
public class UserController  {
     //用户注册
    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add(User user, Model model, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        UserService userService = new UserService();
        user.setId(UUID.randomUUID().toString());
        user.setCreateDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        user.setLastLoginDate(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        userService.add(user);
        model.addAttribute("msg", "index.html");
        return "/sys/user/signin";
    }


    //用户登录
    @RequestMapping(value = "denglu", method = RequestMethod.POST)
    public String submit(String email,String password, Model model, HttpServletResponse response) {
        response.setCharacterEncoding("utf-8");
        UserService userService = new UserService();
        int user1 = userService.denglu(email,password);
        System.out.println(user1);
        if (user1 == 1) {
            model.addAttribute("msg", "成功");
            return "redirect:/index.html";
        } else {
            return "/sys/user/Top";
        }
    }

    @RequestMapping(value = "register")
    public String register(){
        return "/sys/user/signup";
    }


    @RequestMapping(value = "add11")
    public String add11(){
        return "Sumb";
    }

@Autowired
@Qualifier("user")
    private UserService userService;
        //= new UserService();
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public String list(@RequestParam(required = false,defaultValue = "1") Integer pageNum,
                       @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                       Model model){
        //赋值页面页码大小自动帮我们计算，我们只需要在页面上使用PageInfo的数据就行
        PageHelper.startPage(pageNum,pageSize);
        Page<Site> siteList = (Page) userService.findAll1(new User());
        //分页信息
        PageInfo<Site> pageInfo = new PageInfo(siteList);
        model.addAttribute("page",pageInfo);
        return "sys/user/userlist";

    }
    @RequestMapping(value = "findByIdToUpdate")
    public String findByIdToUpdate(String id,Model model){

        User user = userService.findById(id);

        model.addAttribute("user",user);
        return "sys/user/userupdate";
    }
    @RequestMapping(value="update",method = RequestMethod.POST)
    public String update(User user){

        user.setUpdateDate(new Date());
        userService.update(user);
        return "redirect:list?pageNum=1&pageSize=10";
    }
    @RequestMapping(value = "delete",method = RequestMethod.GET)
    public String delete(String id){

        User user = new User();
        user.setId(id);
        userService.delete(user);
        return "redirect:list?pageNum=1&pageSize=10";
    }

}
